package common.tool.box.web.controller.utils.json;

import common.tool.box.common.annotation.Log;
import common.tool.box.common.core.domain.AjaxResult;
import common.tool.box.common.core.domain.model.JsonFormatParam;
import common.tool.box.common.enums.BusinessType;
import common.tool.box.utils.json.JsonFormatUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/utils/json")
@Api(tags = "json")
public class JsonController {
    private static final Logger log = LoggerFactory.getLogger(JsonController.class);

    /**
     * json格式化，type=0随机生成，type=1格式化，type=2压缩，type=3转义，type=4反转义
     */
    @ApiOperation("json格式化，type=0随机生成，type=1格式化，type=2压缩，type=3转义，type=4反转义, type=5转换为Java")
    @PostMapping("/format")
    @Log(title = "json格式化", businessType = BusinessType.TOOL)
    public AjaxResult jsonTextToJson(@RequestBody JsonFormatParam jsonFormatParam) throws Exception {
        try {
            String str = JsonFormatUtils.formatJson(jsonFormatParam);
            AjaxResult ajax = AjaxResult.success();
            ajax.put("result", str);
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }
}
